package com.fenghuajueli.idiomppp.ui.fragment.home;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;

import com.fenghuajueli.idiomppp.database.IdiomDaoUtils;
import com.fenghuajueli.idiomppp.entity.IdiomStoryEntity;
import com.fenghuajueli.idiomppp.entity.VideoInfoEntity;
import com.fenghuajueli.libbasecoreui.mvp.BasePresenter;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class HomepagePresenter extends BasePresenter<HomepageView> {
    CompositeDisposable compositeDisposable = new CompositeDisposable();
    private List<IdiomStoryEntity> idiomStoryEntityList;
    private List<VideoInfoEntity> videoInfoEntities;

    public HomepagePresenter(Context mContext) {
        super(mContext);
        idiomStoryEntityList = new ArrayList<>();
        videoInfoEntities = new ArrayList<>();
    }


    public List<IdiomStoryEntity> getIdiomStoryEntityList() {
        return idiomStoryEntityList;
    }

    /**
     * 初始化成语故事
     */
    public void initStoryData() {
        Observable.create((ObservableOnSubscribe<List<IdiomStoryEntity>>) emitter -> {
            try {
                List<IdiomStoryEntity> list = IdiomDaoUtils.getIdiomStoryManager().listAll();
                idiomStoryEntityList = list.subList(0, 2);
                emitter.onNext(idiomStoryEntityList);
            } catch (Exception e) {
                idiomStoryEntityList = new ArrayList<>();
                emitter.onNext(idiomStoryEntityList);
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<IdiomStoryEntity>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(List<IdiomStoryEntity> idiomStoryEntities) {
                        getView().storyLoadSuccess(idiomStoryEntities);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    public void loadData() {
//        RxHttpUtils.createApi(ApiService.class)
//                .getVideoList("iqy_m_217742214")
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribeOn(Schedulers.io())
//                .compose(Transformer.<BaseData<VideoResponseListEntity>>switchSchedulers())
//                .subscribe(new DataObserver<VideoResponseListEntity>() {
//                    @Override
//                    protected boolean isHideToast() {
//                        return true;
//                    }
//
//                    @Override
//                    protected void onError(String errorMsg) {
//                        LogUtils.d("访问失败---------------,加载本地缓存数据");
//                        String data = MmkvUtils.mmkv.decodeString(MmkvUtils.ConstantsKey.HomeVideoCache.name());
//                        if (TextUtils.isEmpty(data)) {
//                            getView().videoDataError();
//                        } else {
//                            videoInfoEntities = GsonUtils.getGson().fromJson(data, new TypeToken<List<VideoInfoEntity>>() {
//                            }.getType());
//                            if (videoInfoEntities==null) {
//                                videoInfoEntities = new ArrayList<>();
//                            }
//                            getView().dataLoadSuccess(videoInfoEntities);
//                        }
//                    }
//
//                    @Override
//                    protected void onSuccess(VideoResponseListEntity data) {
//                        videoInfoEntities = data.getSearch_lists();
//                        if (videoInfoEntities != null) {
//                            if (videoInfoEntities.size() > 4) {
//                                videoInfoEntities = videoInfoEntities.subList(0, 4);
//                            }
//                            MmkvUtils.mmkv.encode(MmkvUtils.ConstantsKey.HomeVideoCache.name(), GsonUtils.toJson(videoInfoEntities));
//                            getView().dataLoadSuccess(videoInfoEntities);
//                        } else {
//                            getView().videoDataError();
//                        }
//                    }
//                });

    }


    @Override
    public void onDestroy(@NonNull LifecycleOwner owner) {
        super.onDestroy(owner);
        compositeDisposable.dispose();
        compositeDisposable.clear();
    }
}
